﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using System.Reflection;
using Cn.Edwon.Spring;
using Cn.Cellula.Model.System;
using Cn.Cellula.Model.Security;
using Cn.Cellula.Client;
using Cn.Cellula.Biz;

namespace Cn.Cellula.Website.Admin.Content.Atl {
    public partial class EditMultiAtl : PageSupport {
        IItemBiz biz = BizFactory.Get<IItemBiz>();
        String AtlKeyStr = null;
        protected void Page_Load(object sender, EventArgs e) {
            AtlKeyStr = Request.QueryString["idl"];
            if (!Page.IsPostBack) {
                InitForm();
                LoadAtl();
            }
        }
        private void InitForm() {
            IUserBiz ubiz = BizFactory.Get<IUserBiz>();
            IList<TSysRoleEO> list = ubiz.LoadAllRoles(this.Power.RoleLevel);
            this.ddl_EditableRole.DataSource = list;
            this.ddl_EditableRole.DataBind();
            this.ddl_EditableRole.Items.Insert(0, new ListItem("", ""));
            this.ddl_VisiableRole.DataSource = list;
            this.ddl_VisiableRole.DataBind();
            this.ddl_VisiableRole.Items.Insert(0, new ListItem("", ""));
            ClientBiz.BindCodeList(ref this.ddl_CountRule, CodeTypeDef.CountRule.Key, false, "不更改");

        }

        private void LoadAtl() {
            String keystr = "'" + AtlKeyStr.Replace(",", "','") + "'";
            String hql = " from VSysAtlEO eo where eo.AtlKey in (" + keystr + ") ";
            hql += " and( eo.EditableRoleLevel is null or eo.EditableRoleKey='"+this.Power.RoleKey+"' or eo.EditableRoleLevel<=" + this.Power.RoleLevel + ") ";
            hql += " order by eo.OrderNumber desc ";
            IList<VSysAtlEO> list = biz.Find<VSysAtlEO>(hql);
            this.gvlist.DataSource = list;
            this.gvlist.DataBind();
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_save_Click(object sender, EventArgs e) {
            //---- 增加新项 ---------------------------------
            String keystr = "'" + AtlKeyStr.Replace(",", "','") + "'";
            String hql = " from TSysAtlEO eo where eo.AtlKey in (" + keystr + ") order by eo.OrderNumber desc ";
            IList<TSysAtlEO> list = biz.Find<TSysAtlEO>(hql);
            Int32 updated = 0;
            foreach (TSysAtlEO eo in list) {
                updated = 0;
                if (!String.IsNullOrEmpty(this.hid_CategoryKey.Value)) {
                    eo.CategoryKey = this.hid_CategoryKey.Value;
                    updated++;
                }
                if (!String.IsNullOrEmpty(this.ral_Queriable.SelectedValue)) {
                    try {
                        eo.IsQueriable = Convert.ToInt32(this.ral_Queriable.SelectedValue);
                    } catch {
                        eo.IsQueriable = 0;
                    }
                    updated++;
                }

                if (!String.IsNullOrEmpty(this.rbl_IsReport.SelectedValue)) {
                    try {
                        eo.IsReport = Convert.ToInt32(this.rbl_IsReport.SelectedValue);
                    } catch {
                        eo.IsReport = 0;
                    }
                    updated++;
                }
                if (!String.IsNullOrEmpty(this.ddl_CountRule.SelectedValue)) {
                    eo.CountRule = this.ddl_CountRule.SelectedValue;
                    updated++;
                }


                if(updated>0)
                    biz.Update<TSysAtlEO>(eo);

                if (!String.IsNullOrEmpty(this.ddl_VisiableRole.SelectedValue)
                    && !String.IsNullOrEmpty(this.ddl_EditableRole.SelectedValue)) {
                        BizFactory.Get<IUserBiz>().BindPower(eo.AtlKey, this.ddl_VisiableRole.SelectedValue, this.ddl_EditableRole.SelectedValue,null, this.Actor.ID);
                }

            }
            BizFactory.Get<ILogBiz>().Info("批量更新属性模板", this.Actor, Request);
            Response.Redirect("List.aspx?cid=" + Request.QueryString["cid"]);
        }
    }
}